PhysicsSystem Class
The PhysicsSystem is the wrapper around the chipmunk-js physics library that integrates
grapefruit objects into the physics world. It is in charge of managing objects in the physics
space. Generally you would not create this yourself and instead would use the .physics property
of a State.
Constructor
Item Index
Methods
Properties
Methods
_createBody
-
spr
Creates a physics body for a sprite
Parameters:
-
sprSpriteThe sprite to create a body for
Returns:
The chipmunk-js physics body
_createShape
-
spr -
body -
[poly]
Creates a collision shape for a sprite
Parameters:
Returns:
The chipmunk-js collision shape
add
-
spr -
[callback]
Adds a sprite to the physics simulation
Parameters:
-
sprSpriteThe sprite to add
-
[callback]Function optionalThe callback to call once the sprite has been added
Returns:
The sprite that was added
addCustomShape
-
spr -
poly -
sensor -
[callback]
Adds a custom shape to a sprite, useful for a single sprite to have multiple different collision shapes (including sensors).
Parameters:
Returns:
The shape that was created
getCollisionType
-
spr
Returns the collision type of a sprite
Parameters:
-
sprSpriteThe sprite to check
Returns:
The collision type
nextTick
-
fn
Registers a callback to be executed on the next frame step
Parameters:
-
fnFunctionThe callback to register
Returns:
Returns itself.
onCollisionBegin
-
arbiter -
space
Called when a collision begins in the system
Parameters:
-
arbitercp.ArbiterThe arbiter of the collision
-
spacecp.SpaceThe space the collision occurred in
onCollisionEnd
()
private
Attempts to perform the postStep actions that have been queued. If the space is currently locked, then it waits until after the step to run the actions.
onCollisionEnd
-
arbiter -
space
Called after a collision ends in the system (separation)
Parameters:
-
arbitercp.ArbiterThe arbiter of the collision
-
spacecp.SpaceThe space the collision occurred in
onCollisionPostSolve
-
arbiter -
space
Called after a collision is solved in the system
Parameters:
-
arbitercp.ArbiterThe arbiter of the collision
-
spacecp.SpaceThe space the collision occurred in
onPostStep
()
private
Processes the action queue after a step is unlocked.
reindex
-
spr -
[callback]
Reindexes a sprite's shape in the simulation, useful if it looks like changes are being cached.
Parameters:
-
sprSpriteThe sprite to reindex
-
[callback]Function optionalThe callback to call once the sprite has been reindexed
Returns:
Returns itself.
reindexStatic
-
[callback]
Reindexes all static bodies in the simulation.
Parameters:
-
[callback]Function optionalThe callback to call once reindexing completes
Returns:
Returns itself.
remove
-
spr -
[callback]
Removes a sprite from the physics simulation
Parameters:
-
sprSpriteThe sprite to remove
-
[callback]Function optionalThe callback to call once the sprite has been removed
Returns:
The sprite that was removed
resume
()
PhysicsSystem
chainable
Resumes physics simulation
Returns:
Returns itself.
setMass
-
spr -
mass
Sets the mass of a sprite's physics body.
Parameters:
-
sprSpriteThe sprite to set the mass for
-
massNumberThe mass to set
Returns:
Returns itself.
setPosition
-
spr -
position
Sets the position of a sprite's physics body.
Returns:
Returns itself.
setRotation
-
spr -
rotation
Sets the rotation of a sprite's physics body.
Parameters:
-
sprSpriteThe sprite to set the rotation for
-
rotationNumberThe rotation to set to in radians
Returns:
Returns itself.
setVelocity
-
spr -
velocity
Sets the velocity of a sprite's physics body.
Returns:
Returns itself.
skip
-
num
Skips the specified number of frame steps
Parameters:
-
numNumberNumber of steps to skip
Returns:
Returns itself.
skipNext
()
PhysicsSystem
chainable
Skips the next frame step
Returns:
Returns itself.
update
-
dt
Called each frame by the game state to update the physics simulations
Parameters:
-
dtNumberThe number of seconds passed since the last call
Properties
_skip
Number
private
The number of steps to skip, tracks this.skip(num)
actonQueue
Array
private
The actions to perform at the next post-step callback. These are for actions (like add, remove) that cannot be performed during a simulation step.
space
cp.Space
The chipmunk space instance that will run all the physics simulations
tickCallbacks
ArrayThe callback functions to call on the next frame